/**
 * Copyright (c) 2010 Claudio Imbrenda.
 * This program is released under the terms of the GNU GPL License, version 3 or later.
 */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "graph.h"

static void usage(){
 fprintf(ERR,"usage:\tmkshmgraph [-b|-t] inputfile outputfile");
 fprintf(ERR,"\t-b\tthe input graph is in binary format [default]\n");
 fprintf(ERR,"\t-t\tthe input graph is in text format\n");
}

/** Converts the given graph (in text or binary format) into SHM format.
*/
int main(int argc, char*argv[]){
struct tngraph*thegraph;
int mode = GRAPH_MODE_BINARY;
char*infn,*outfn;
FILE* outf;
int a,b,cx;

 if((argc<3)||(argc>4)){
   usage();
   exit(1);
 }
 
 if(argc==4){
  if(!strncmp("-t",argv[1],3)){
   mode=GRAPH_MODE_TEXT;
  }else if(strncmp("-b",argv[1],3)){
   usage();
   exit(1);
  }
  infn=argv[2];
  outfn=argv[3];
 } else{
  infn=argv[1];
  outfn=argv[2];
 }
 
 thegraph = loadgraph(infn,GRAPH_MODE_BINARY);
 
 outf = fopen(outfn,"w");
 if(outf==NULL){
  perror("cannot open output file");
  exit(1);
 }
 
 a=thegraph->size;
 if(fwrite(&a,sizeof(uint),1,outf)!=1){
  perror("error during write");
  exit(1);
 }
 
 for(cx=0;cx<thegraph->size;cx++){
  b=thegraph->nodes[cx].size;
  if(fwrite(&b,sizeof(uint),1,outf)!=1){
   perror("error during write");
   exit(1);
  } 
 }
 
 for(cx=0;cx<thegraph->size;cx++){
  a=thegraph->nodes[cx].size;
  if(fwrite((thegraph->nodes[cx].neighbours),sizeof(uint),a,outf)!=a){
   perror("error during write");
   exit(1);  
  }
 }
 fclose(outf);
 return 0;
}

